package com.t4.wechat.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.t4.model.wechat.WechatMenu;
import com.t4.vo.wechat.WechatMenuVo;
import com.t4.wechat.mapper.WechatMenuMapper;
import com.t4.wechat.service.WechatMenuService;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class WechatMenuServiceImpl extends ServiceImpl<WechatMenuMapper, WechatMenu> implements WechatMenuService {

    @Resource
    private WxMpService wxMpService;

    @Override
    public List<WechatMenuVo> findMenuInfo() {
        // 1. 查询所有菜单list集合
        List<WechatMenu> allWechatMenuList = baseMapper.selectList(null);
        // 2. 查询所有一级菜单（parentId=0）
        List<WechatMenu> rootWechatMenuList = baseMapper.selectList(new LambdaQueryWrapper<WechatMenu>().eq(WechatMenu::getParentId, 0));
        // 3. 转化成WechatMenuVo
        List<WechatMenuVo> result = new ArrayList<>();
        for(WechatMenu parentWechatMenu: rootWechatMenuList){
            WechatMenuVo parentWechatMenuVo = new WechatMenuVo();
            BeanUtils.copyProperties(parentWechatMenu, parentWechatMenuVo);
            List<WechatMenuVo> children = new ArrayList<>();
            for(WechatMenu childWechatMenu: allWechatMenuList){
                if(childWechatMenu.getParentId() == parentWechatMenu.getId()){
                    WechatMenuVo childWechatMenuVo = new WechatMenuVo();
                    BeanUtils.copyProperties(childWechatMenu, childWechatMenuVo);
                    children.add(childWechatMenuVo);
                }
            }
            parentWechatMenuVo.setChildren(children);
            result.add(parentWechatMenuVo);
        }

        return result;
    }

    @Override
    public void syncMenu() {
        // 1. 查询出菜单数据，封装成微信要求的菜单格式
        // 1.1 从数据库查询出封装好的树型菜单数据
        List<WechatMenuVo> menuVoList = this.findMenuInfo();
        // 1.2 按照微信要求的格式进行菜单的封装
        JSONArray buttonList = new JSONArray();
        for(WechatMenuVo oneMenuVo: menuVoList){
            JSONObject one = new JSONObject();
            one.put("name", oneMenuVo.getName());
            if(CollectionUtils.isEmpty(oneMenuVo.getChildren())){
                one.put("type", oneMenuVo.getType());
                one.put("url", "http://114.116.11.34:9090/#" + oneMenuVo.getUrl());
            }else{
                JSONArray subButton = new JSONArray();
                for(WechatMenuVo twoMenuVo: oneMenuVo.getChildren()){
                    JSONObject view = new JSONObject();
                    view.put("type", twoMenuVo.getType());
                    if(twoMenuVo.getType().equals("view")) {
                        view.put("name", twoMenuVo.getName());
                        // H5页面地址
                        view.put("url", "http://114.116.11.34:9090/#" + twoMenuVo.getUrl());
                    } else {
                        view.put("name", twoMenuVo.getName());
                        view.put("key", twoMenuVo.getMeunKey());
                    }
                    subButton.add(view);
                }
                one.put("sub_button", subButton);
            }
            buttonList.add(one);
        }
        JSONObject button = new JSONObject();
        button.put("button", buttonList);

        // 2. 调用微信依赖提供的方法实现菜单推送
        try {
            wxMpService.getMenuService().menuCreate(button.toString());
        } catch (WxErrorException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void removeMenu() {
        try {
            wxMpService.getMenuService().menuDelete();
        } catch (WxErrorException e) {
            e.printStackTrace();
        }
    }


}
